package com.zqq.glide.myview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.zqq.glide.R;

/**
 * @author zqq on 2018/7/11
 */
public class DialogMaxHeight extends Dialog implements View.OnClickListener {

    private DialogView dialogView;

    DialogMaxHeight(@NonNull Context context, DialogView dialogView) {
        super(context, R.style.dialog);
        this.dialogView = dialogView;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        initWindow();
        initClick();
    }


    private void initWindow() {
        Window window = this.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;

        window.setAttributes(params);

//        this.setCancelable(false);//false:均不可以

        this.setCanceledOnTouchOutside(false);//true:空白与返回键均可消失,false：空白处不可以返回键可以
    }

    TextView textView;
    private void initClick() {
        textView= findViewById(R.id.title);
        textView .setOnClickListener(this);
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int idx = v.getId();
        switch (idx) {
            case R.id.title:
                Toast.makeText(getContext(), textView.getText().toString(), Toast.LENGTH_SHORT).show();
                textView.setText("textView");
                break;
            case R.id.btn1:
                Toast.makeText(getContext(), "1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn2:
                textView.setText("btn2");
                break;
            case R.id.btn3:
                Toast.makeText(getContext(), "btn3", Toast.LENGTH_SHORT).show();
                dialogView.setText("来自Dialog");
                break;
            case R.id.btn4:
                dialogView.startActivity();
                break;
            default:
                break;
        }

    }

}
